// components/shop.js

import url from "../../util/url.js"

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  lifetimes: {
    attached: function () {
      this.updateUserInfo()
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    goods:[],
    userId: '',
    openId: '',
  },

  lifetimes: {
    attached: function () {
      this.updateUserInfo()
      wx.request({
        url: url.goodsList,
        success : (res) => {
          

          let k = 0, j = 0;
          let array = [];
          for (let i = 0; i < res.data.length; i++) {
            let item = res.data[i];
            if (!array[k]) {
              array[k] = []
            }
            array[k][j] = item;
            j = (j + 1) % 2
            if (j == 0) {
              k++
            }
            this.setData({
              goods: array
            })
          }

        }
      })
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    itemClick:function(e){
      // goodsPrice
      console.log(e.currentTarget)

      wx.showLoading({
        title: '',
        mask: true
      })
      wx.request({
        url: url.miniPayBuy,
        method: 'POST',
        data: {
          totalAmount: e.currentTarget.dataset.good.goodsPrice,
          // totalAmount: '0.01',
          merchantNo: this.data.userId,
          appId: 'wx5298b0879c8fa8c4',
          openId: this.data.openId,
          userId: this.data.userId,
          returnParams: this.data.userId + "," + e.currentTarget.dataset.good.id
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: (res) => {
          if (!res.data || res.data.code != 200) {
            wx.showModal({
              title: '提示',
              content: res.data.message || res.data,
              showCancel: false
            })
          } else {
            let payObj = JSON.parse(res.data.data.preparePayInfo);
            wx.requestPayment(
              {
                'timeStamp': payObj.timeStamp,
                'nonceStr': payObj.nonceStr,
                'package': payObj.package,
                'signType': payObj.signType,
                'paySign': payObj.paySign,
                'success': res => { 
                  wx.showModal({
                    title: '提示',
                    content: '购买成功',
                    showCancel: false
                  })
                 },
                'fail': res => { 
                  wx.showModal({
                    title: '提示',
                    content: '购买失败',
                    showCancel: false
                  })
                 },
              })
          }

        },
        complete: () => {
          wx.hideLoading()
        }
      })

    },

    updateUserInfo: function () {
      console.log('updateUserInfo')
      wx.getStorage({
        key: 'openId',
        success: (res) => {
          console.log(res)
          this.setData({
            openId: res.data
          })
        },
      })
      wx.getStorage({
        key: 'userId',
        success: (res) => {
          this.setData({
            userId: res.data
          })
        },
      })
    }
     

  }
})
